//
//  ViewController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/8/4.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "ViewController.h"
#import "sys/utsname.h"
#include <sys/types.h>
#include <sys/sysctl.h>
/**
 限制条件:
 Core NFC 目前只支持  NFC tag的读取  不支持输出和格式设置
 仅支持iphone 7 & 7plus  切ios11以上系统
 
 //<NFCNDEFReaderSessionDelegate>
 */

@import CoreNFC;//导入nfc框架
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"UI";
    self.view.backgroundColor = [UIColor whiteColor];
    // 创建 NFCNDEFReaderSession 实例，开启NFCNDEFReaderSession
    // Tips：开启
    // 条件：iphone7/7plus运行iOS11  if ([deviceString isEqualToString:@"iPhone9,1"])    return @"";
    if ([self isiPhone7oriPhone7Plus] && IOS11_OR_LATER) {
        // ReadingAvailable is YES if device supports NFC tag reading.
        if (@available(iOS 11.0, *)) {
            if ([NFCNDEFReaderSession readingAvailable]) {
                // beginScanning
                // invalidateAfterFirstRead 属性表示是否需要识别多个NFC标签，如果是YES，则会话会在第一次识别成功后终止。否则会话会持续
                // 不过有一种例外情况，就是如果响应了-readerSession:didInvalidateWithError:方法，则是否为YES，会话都会被终止
//                NFCNDEFReaderSession *session = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:nil invalidateAfterFirstRead:YES];
//                
//                [session beginSession];
            }
        } else {
            // Fallback on earlier versions
        }
    } else {
        // 主动终止会话，调用如下方法即可。
        //[session invalidateSession];
    }
}
// 处理协议回调方法
#pragma mark - NFCReaderSessionDelegate

// Check invalidation reason from the returned error. A new session instance is required to read new tags.
// 识别出现Error后会话会自动终止，此时就需要程序重新开启会话
//- (void)readerSession:(NFCNDEFReaderSession *)session didInvalidateWithError:(NSError *)error {
//    // error明细参考NFCError.h
//    NSLog(@"%@",error);
//}
// Process detected NFCNDEFMessage objects
//- (void)readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray<NFCNDEFMessage *> *)messages {
//    // 数组messages中是NFCNDEFMessage对象
//    // NFCNDEFMessage对象中有一个records数组，这个数组中是NFCNDEFPayload对象
//    // 参考NFCNDEFMessage、NFCNDEFPayload类
//    // 解析数据
//    if (@available(iOS 11.0, *)) {
//        for (NFCNDEFMessage *message in messages) {
//            for (NFCNDEFPayload *playLoad in message.records) {
//                NSLog(@"typeNameFormat : %d", playLoad.typeNameFormat);
//                NSLog(@"type : %@", playLoad.type);
//                NSLog(@"identifier : %@", playLoad.identifier);
//                NSLog(@"playload : %@", playLoad.payload);
//            }
//        }
//    } else {
//        // Fallback on earlier versions
//    }
//}
#pragma mark---Help
-(BOOL)isiPhone7oriPhone7Plus{
    
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    if ([deviceString isEqualToString:@"iPhone9,1"]||[deviceString isEqualToString:@"iPhone9,2"]){
        return YES;
    }
    return NO;

}

@end
